#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-10-19 9:39 上午
# @Author  : yangdy
# @File    : 2020-10-19.844.比较含退格的字符串
# @Software: PyCharmCE


class Solution:
    def backspaceCompare(self, S: str, T: str) -> bool:

        def work(s):
            stack, i = [], 0
            while i < len(s):
                if s[i] == '#':
                    if stack:
                        stack.pop()
                else:
                    stack.append(s[i])
                i += 1
            return ''.join(stack)

        return work(S) == work(T)
